package cn.edu.kzjsxy.graduationproject.controller;


import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.common.TokenUtil;
import cn.edu.kzjsxy.graduationproject.entity.Buildings;
import cn.edu.kzjsxy.graduationproject.entity.Units;
import cn.edu.kzjsxy.graduationproject.entity.Users;
import cn.edu.kzjsxy.graduationproject.service.IBuildingsService;
import cn.edu.kzjsxy.graduationproject.service.IUnitsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 楼栋信息表 前端控制器
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Api(tags = "楼栋信息表")
@RestController
@RequestMapping("/buildings")
public class BuildingsController {
//	单元的service
	@Autowired
	private IUnitsService unitsService;

//	楼栋信息的service
	@Autowired
	private IBuildingsService buildingsService;

//	tokenUitl
	@Autowired
	private TokenUtil tokenUtil;

	@GetMapping("/{buildingId}")
	@ApiOperation("根据楼栋ID查询楼栋信息")
	public Result<Buildings> getDormitoriesById(@PathVariable String buildingId, HttpServletRequest request) {
		Users users = tokenUtil.resolveToken(request);
		Buildings buildings = buildingsService.getById(buildingId);
		buildings=buildingsService.getHousemasterNameByHousemasterId(buildings);
		return Result.success(buildings);
	}
	@ApiOperation("添加楼栋信息")
	@PostMapping("/add")
	public Result addBuilding(@RequestBody Buildings building) {
		boolean success = buildingsService.save(building);
		return Result.judgment(success);
	}

	@ApiOperation("删除楼栋信息")
	@DeleteMapping("/delete/{buildingId}")
	public Result deleteBuilding(@PathVariable String buildingId) {
		List<Units> unitsList = unitsService.selectUnitsByCondition(Units.builder().buildingId(buildingId).build());
		if(unitsList!=null&&!unitsList.isEmpty()){
			return Result.error("当前楼栋存在单元信息，无法删除!");
		}
		boolean success = buildingsService.removeById(buildingId);
		return Result.judgment(success);
	}

	@ApiOperation("更新楼栋信息")
	@PutMapping("/update")
	public Result updateBuilding(@RequestBody Buildings building) {
		boolean success = buildingsService.updateById(building);
		return Result.judgment(success);
	}

	@ApiOperation("获取所有楼栋信息")
	@PostMapping("/list")
	public Result<List<Buildings>> getAllBuildings(@RequestBody Buildings building,HttpServletRequest request) {
		List<Buildings> buildingsList = buildingsService.selectBuildingsByCondition(building,request);
		return Result.success(buildingsList);
	}

	@ApiOperation("分页获取楼栋信息")
	@PostMapping("/page")
	public Result<PageInfo<Buildings>> getBuildingsByPage(Page page, @RequestBody Buildings building,HttpServletRequest request){
		// 此处可以根据需要进行分页查询
		return	buildingsService.selectBuildingsByPage(page,building,request);
	}



}
